/**
 * 
 */
package com.gmail.blackdog1987.onedollar.web.controller;

import com.gmail.blackdog1987.onedollar.service.CartService;
import com.gmail.blackdog1987.onedollar.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 
 * @author 董国超
 *
 */
@Controller
public class CartController extends BaseController {
    @Autowired
    CartService cartService;

    /**
     * 查看购物车
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="getCart", method=RequestMethod.POST)
    public Map<String, Object> getCart(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = cartService.getCart(filter);
        return decorateResult(result);
    }

    /**
     * 清空购物车
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="emptyCart", method=RequestMethod.POST)
    public Map<String, Object> emptyCart(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = cartService.deleteAllGoods(filter);
        return decorateResult(result);
    }

    /**
     * 删除商品
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="deleteCart", method=RequestMethod.POST)
    public Map<String, Object> deleteCart(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result =  cartService.deleteGoods(filter);
        return decorateResult(result);
    }

    /**
     * 更新购物车。更改数量（增加商品数量，删除商品数量）
     * @param filter
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value="updateCart", method=RequestMethod.POST)
    public Map<String, Object> deleteAddress(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = cartService.updateGoodsCount(filter);
        return decorateResult(result);
    }

    @Override
    protected Map<String, Object> decorateResult(Map<String, Object> result) {
        result.put("time", System.currentTimeMillis());
        return result;
    }
}
